home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / rmtlog1a / eventlog.cls < prev    next >
Text File  |  1999-03-21  |  4KB  |  168 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "EventLog"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  16. Option Explicit
  17.  
  18. Private mstrUserName As String
  19. Private mstrComputerName As String
  20. Private mstrEventSource As String
  21. Private mstrLogDestination As String
  22. Private mbEnabled As Boolean
  23.  
  24. Private mobjFile As CFileOutputHandler
  25. Public Property Get ComputerName() As String
  26.  
  27.   ComputerName = mstrComputerName
  28.  
  29. End Property
  30.  
  31. Friend Property Get OutputFile() As CFileOutputHandler
  32.   
  33.   If mbEnabled Then
  34.     Set OutputFile = mobjFile
  35.   End If
  36.  
  37. End Property
  38.  
  39. Public Property Get UserName() As String
  40.   
  41.   UserName = mstrUserName
  42.  
  43. End Property
  44. Public Function WriteLog(Message As String, EventType As LogEventTypes, EventID As Long) As Boolean
  45.  
  46.   Dim objLine As DelimitedString
  47.  
  48.   If mbEnabled = False Then
  49.     Err.Raise vbObjectError + ERR_WRITELOG_DISABLED, App.Title, "Can't write to log when Enabled = False"
  50.     WriteLog = False
  51.     Exit Function
  52.   End If
  53.   
  54.   Set objLine = New DelimitedString
  55.   
  56.   With objLine
  57.     .Add CStr(Now)
  58.     .Add mstrEventSource
  59.     .Add CStr(EventType)
  60.     .Add Message
  61.     .Add mstrUserName
  62.     .Add mstrComputerName
  63.   End With
  64.   
  65.   WriteLog = mobjFile.WriteLine(objLine.Value)
  66.   
  67.   Set objLine = Nothing
  68.  
  69. End Function
  70.  
  71.  
  72. Public Property Let Enabled(ByVal NewEnabled As Boolean)
  73.  
  74.   If NewEnabled = mbEnabled Then
  75.     Exit Property
  76.   End If
  77.   
  78.   If mbEnabled Then
  79.     mobjFile.CloseFile
  80.     Set mobjFile = Nothing
  81.     mbEnabled = False
  82.   Else
  83.     If Trim(mstrLogDestination) = "" Then
  84.       Err.Raise vbObjectError + ERR_NO_LOGDEST, App.Title, "Can't set Enabled to True without a Log Destination"
  85.       Exit Property
  86.     End If
  87.     If Trim(mstrEventSource) = "" Then
  88.       Err.Raise vbObjectError + ERR_NO_SOURCE, App.Title, "Can't set Enabled to True without an Event Source"
  89.       Exit Property
  90.     End If
  91.     Set mobjFile = New CFileOutputHandler
  92.     If mobjFile.OpenFile(mstrLogDestination) = False Then
  93.       Err.Raise vbObjectError + ERR_FILEOPEN_FAILED, App.Title, "Couldn't open file '" & mstrLogDestination & "'"
  94.       Exit Property
  95.     Else
  96.       mbEnabled = True
  97.     End If
  98.   End If
  99.  
  100. End Property
  101. Public Property Get Enabled() As Boolean
  102.  
  103.   Enabled = mbEnabled
  104.  
  105. End Property
  106. Public Property Let LogDestination(ByVal NewLogDest As String)
  107.  
  108.   If mbEnabled Then
  109.     Err.Raise vbObjectError + ERR_LOGDEST_DISABLED, App.Title, "Can't change LogDestination while Enabled property = True"
  110.     Exit Property
  111.   End If
  112.   
  113.   If Trim(NewLogDest) = "" Then
  114.     Err.Raise vbObjectError + ERR_LOGDEST_NULL, App.Title, "LogDestination can't be a zero-length string"
  115.     Exit Property
  116.   End If
  117.  
  118.   mstrLogDestination = NewLogDest
  119.  
  120. End Property
  121.  
  122. Public Property Get LogDestination() As String
  123.  
  124. End Property
  125.  
  126. Public Property Let EventSource(ByVal NewSource As String)
  127.  
  128.   If mbEnabled Then
  129.     Err.Raise vbObjectError + ERR_SOURCE_DISABLED, App.Title, "Can't set EventSource property while Enabled = True"
  130.     Exit Property
  131.   End If
  132.  
  133.   If Trim(NewSource) = "" Then
  134.     Err.Raise vbObjectError + ERR_SOURCE_NULL, App.Title, "EventSource can't be a zero-length string"
  135.     Exit Property
  136.   End If
  137.  
  138.   mstrEventSource = NewSource
  139.  
  140. End Property
  141. Public Property Get EventSource() As String
  142.  
  143.   EventSource = mstrEventSource
  144.  
  145. End Property
  146.  
  147. Private Sub Class_Initialize()
  148.  
  149.   Dim lpBuffer As String
  150.   Dim nSize As Long
  151.   Dim nRetVal As Long
  152.   
  153.   nSize = 255
  154.   lpBuffer = Space(nSize)
  155.   nRetVal = GetUserName(lpBuffer, nSize)
  156.   mstrUserName = Left(lpBuffer, nSize - 1)
  157.   
  158.   nSize = 255
  159.   lpBuffer = Space(nSize)
  160.   nRetVal = GetComputerName(lpBuffer, nSize)
  161.   mstrComputerName = Left(lpBuffer, nSize)
  162.  
  163. End Sub
  164.  
  165.  
  166.  
  167.  
  168.